package com.sens.ipc.mybank.hooker;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.sens.ipc.common.CommonAction;
import com.sens.ipc.mybank.Action;
import com.sens.ipc.mybank.application.Handle;

public class Proxy {
    private Context context;
    private Callback callback;

    public Proxy(Context context, Callback hookerCallback) {
        this.context = context;
        if (hookerCallback == null) return;
        this.callback = hookerCallback;
        this.hookerReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent != null)
                    switch (intent.getAction()) {
                        case Action.Hooker.ACTION_QUERY_ORDER:
                            callback.queryOrder();
                            break;
                        case Action.Hooker.ACTION_SET_CHANNEL_NUMBER:
                            callback.setChannelNumber(intent.getStringExtra(intent.getAction()));
                            break;
                        case Action.Hooker.ACTION_SET_QUERY_TIME_INTERVAL:
                            callback.setQueryTimeInterval(intent.getStringExtra(intent.getAction()));
                            break;
                        case Action.Hooker.ACTION_CHECK_USERID:
                            callback.checkUserId();
                            break;
                        case Action.Hooker.ACTION_START:
                            callback.start();
                            break;
                        case Action.Hooker.ACTION_STOP:
                            callback.stop();
                            break;
                        case CommonAction.Hooker.ACTION_SET_HOST:
                            callback.setHost(intent.getStringExtra(intent.getAction()));
                            break;
                    }
            }
        };
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Action.Hooker.ACTION_QUERY_ORDER);
        intentFilter.addAction(Action.Hooker.ACTION_SET_CHANNEL_NUMBER);
        intentFilter.addAction(Action.Hooker.ACTION_SET_QUERY_TIME_INTERVAL);
        intentFilter.addAction(Action.Hooker.ACTION_CHECK_USERID);
        intentFilter.addAction(Action.Hooker.ACTION_START);
        intentFilter.addAction(Action.Hooker.ACTION_STOP);
        intentFilter.addAction(CommonAction.Hooker.ACTION_SET_HOST);
        context.registerReceiver(hookerReceiver, intentFilter);
    }

    private BroadcastReceiver hookerReceiver;

    public Handle getHandle() {
        return new Handle(context) {
            @Override
            public void unBind() {
                if (hookerReceiver != null)
                    context.unregisterReceiver(hookerReceiver);
                hookerReceiver = null;
                context = null;
                callback = null;
            }
        };
    }
}
